package com.company.LifeCycleThreadPool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 作者  Administrator
 * 时间  2017/6/2 13:29
 * 文件  ThreadPoolsDemo
 * 描述  管理线程池的生命周期
 */
public class MyThreadPoolExecutor extends ThreadPoolExecutor {
	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
	                            long keepAliveTime, TimeUnit unit,
	                            BlockingQueue<Runnable> workQueue) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
	}

	/**
	 * @param t 这里是对线程而言(而不是线程池)
	 * @param r
	 */
	@Override
	protected void beforeExecute(Thread t, Runnable r) {
		super.beforeExecute(t, r);
		String threadName = t.getName();
		System.out.println("线程：" + threadName + "准备执行任务！");
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		super.afterExecute(r, t);
		String threadName = Thread.currentThread().getName();
		System.out.println("线程：" + threadName + "任务执行结束！");
	}

	/**
	 * 此处是对线程池而言的
	 */
	@Override
	protected void terminated() {
		super.terminated();
		System.out.println("线程池结束！");
	}

}